package com.xiaoyu.lingdian.controller;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.xiaoyu.lingdian.tool.DateUtil;
import com.xiaoyu.lingdian.tool.RandomUtil;
import com.xiaoyu.lingdian.tool.StringUtil;
import javax.servlet.http.HttpServletResponse;
import com.xiaoyu.lingdian.core.mybatis.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import com.xiaoyu.lingdian.tool.out.ResultMessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import com.xiaoyu.lingdian.controller.BaseController;
import com.xiaoyu.lingdian.entity.CoreAttachment;
import com.xiaoyu.lingdian.entity.CoreIntegral;
import com.xiaoyu.lingdian.entity.CoreRideActive;
import com.xiaoyu.lingdian.entity.CoreRideRecord;
import com.xiaoyu.lingdian.service.CoreAttachmentService;
import com.xiaoyu.lingdian.service.CoreIntegralService;
import com.xiaoyu.lingdian.service.CoreRideActiveService;
import com.xiaoyu.lingdian.service.CoreRideRecordService;
import com.xiaoyu.lingdian.vo.CoreRideActiveVO;

@Controller
@RequestMapping(value="/coreRideActive")
public class CoreRideActiveController extends BaseController {

	/**
	* CoreRideActive表
	*/
	@Autowired
	private CoreRideActiveService coreRideActiveService;
	
	/**
	 * 附件表
	 */
	@Autowired
	private CoreAttachmentService coreAttachmentService;
	
	/**
	 * 积分攻略表
	 */
	@Autowired
	private CoreIntegralService coreIntegralService;
	
	/**
	* CoreRideRecord表
	*/
	@Autowired
	private CoreRideRecordService coreRideRecordService;
	
	/**
	* 添加（添加附件）
	*
	* @param crraeTitle 标题
	* @param crraeContent 内容
	* @param crraeValue 价格
	* @param uuids
	* @return
	*/
	@RequestMapping(value="/add/coreRideActive", method=RequestMethod.POST)
	public void addCoreRideActive (String crraeTitle, String crraeContent, String crraeValue, String uuids, HttpServletResponse response) {
		logger.info("[CoreRideActiveController]:begin addCoreRideActive");
		if (StringUtils.isBlank(crraeTitle)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入热闹活动标题！"), response);
			return;
		}
		if (StringUtils.isBlank(crraeContent)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入热闹活动内容！"), response);
			return;
		}
		if (StringUtils.isBlank(crraeValue)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入热闹活动价格！"), response);
			return;
		}
		if (("0").equals(crraeValue)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "热闹活动价格不能为0！"), response);
			return;
		}		CoreRideActive coreRideActive = new CoreRideActive();
		String uuid = RandomUtil.generateString(16);
		coreRideActive.setCrraeUuid(uuid);
		coreRideActive.setCrraeTitle(crraeTitle);
		coreRideActive.setCrraeContent(crraeContent);
		coreRideActive.setCrraeValue(crraeValue);
		coreRideActive.setCrraeCdate(new Date());
		coreRideActive.setCrraeUdate(new Date());

		coreRideActiveService.insertCoreRideActive(coreRideActive);

		// 添加凑热闹封面
		if (!StringUtils.isBlank(uuids)) {
			CoreAttachment coreAttachment = new CoreAttachment();
			String[] uuidModel = uuids.split("\\|");
			for (int i = 0; i < uuidModel.length; i++) {
				coreAttachment = new CoreAttachment();
				coreAttachment.setCratmUuid(uuidModel[i]);
				coreAttachment.setCratmBusUuid(uuid);
				coreAttachmentService.updateCoreAttachment(coreAttachment);
			}
		}
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "新增成功!"),response);
		logger.info("[CoreRideActiveController]:end addCoreRideActive");
	}

	/**
	* 修改
	* 
	* @param crraeUuid 标识UUID
	* @param crraeTitle 标题
	* @param crraeContent 内容
	* @param crraeValue 价格
	* @param uuids
	* 
	* @return
	*/
	@RequestMapping(value="/update/coreRideActive", method=RequestMethod.POST)
	public void updateCoreRideActive (String crraeUuid, String crraeTitle, String crraeContent, String crraeValue, String uuids, HttpServletResponse response) {
		logger.info("[CoreRideActiveController]:begin updateCoreRideActive");
		if (StringUtil.isEmpty(crraeUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		if (StringUtils.isBlank(crraeTitle)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入热闹活动标题！"), response);
			return;
		}
		if (StringUtils.isBlank(crraeContent)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入热闹活动内容！"), response);
			return;
		}
		if (StringUtils.isBlank(crraeValue)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入热闹活动价格！"), response);
			return;
		}
		if (("0").equals(crraeValue)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "热闹活动价格不能为0！"), response);
			return;
		}		CoreRideActive coreRideActive = new CoreRideActive();
		coreRideActive.setCrraeUuid(crraeUuid);
		coreRideActive.setCrraeTitle(crraeTitle);
		coreRideActive.setCrraeContent(crraeContent);
		coreRideActive.setCrraeValue(crraeValue);
		coreRideActive.setCrraeUdate(new Date());

		coreRideActiveService.updateCoreRideActive(coreRideActive);

		// 修改凑热闹封面
		if (!StringUtils.isBlank(uuids)) {
			CoreAttachment coreAttachment = new CoreAttachment();
			String[] uuidModel = uuids.split("\\|");
			for (int i = 0; i < uuidModel.length; i++) {
				coreAttachment = new CoreAttachment();
				coreAttachment.setCratmUuid(uuidModel[i]);
				coreAttachment.setCratmBusUuid(crraeUuid);
				coreAttachmentService.updateCoreAttachment(coreAttachment);
			}
		}
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "修改成功!"),response);
		logger.info("[CoreRideActiveController]:end updateCoreRideActive");
	}

	/**
	* 删除
	*
	* @param crraeUuid 标识UUID
	* @return
	*/
	@RequestMapping(value="/delete/one", method=RequestMethod.POST)
	public void deleteCoreRideActive (String crraeUuid, HttpServletResponse response) {
		logger.info("[CoreRideActiveController]:begin deleteCoreRideActive");
		if (StringUtil.isEmpty(crraeUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		CoreRideActive coreRideActive = new CoreRideActive();
		coreRideActive.setCrraeUuid(crraeUuid);

		coreRideActiveService.deleteCoreRideActive(coreRideActive);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "删除成功!"),response);
		logger.info("[CoreRideActiveController]:end deleteCoreRideActive");
	}

	/**
	* 批量删除
	*
	* @param crraeUuids UUID集合

	* @return
	*/
	@RequestMapping(value="/delete/batch", method=RequestMethod.POST)
	public void deleteBatchCoreRideActive (String crraeUuids, HttpServletResponse response) {
		logger.info("[CoreRideActiveController]:begin deleteBatchCoreRideActive");
		if (StringUtil.isEmpty(crraeUuids)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[UUID集合]不能为空!"), response);
			return;
		}
		String[] uuids=crraeUuids.split("\\|");
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < uuids.length; i++) {
			list.add(uuids[i]);
		}
		if (list.size() == 0) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择批量删除对象！"), response);
			return;
		}
		coreRideActiveService.deleteBatchByIds(list);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "批量删除成功!"),response);
		logger.info("[CoreRideActiveController]:end deleteBatchCoreRideActive");
	}

	/**
	* 获取单个
	*
	* @param crraeUuid 标识UUID
	* @param userUuid
	* @return
	*/
	@RequestMapping(value="/views", method=RequestMethod.POST)
	public void viewsCoreRideActive (String crraeUuid, String userUuid, HttpServletResponse response) {
		logger.info("[CoreRideActiveController]:begin viewsCoreRideActive");
		if (StringUtil.isEmpty(crraeUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		CoreRideActive coreRideActive = new CoreRideActive();
		coreRideActive.setCrraeUuid(crraeUuid);

		coreRideActive = coreRideActiveService.getCoreRideActive(coreRideActive);

		CoreRideActiveVO coreRideActiveVO = new CoreRideActiveVO();
		coreRideActiveVO.convertPOToVO(coreRideActive);
		
		//获取相应的积分
		int points = 0;
		CoreIntegral coreIntegral = new CoreIntegral();
		coreIntegral.setCriglUuid("BBBBB");
		coreIntegral = coreIntegralService.getCoreIntegral(coreIntegral);
		if (coreIntegral != null) {
			String criglScore = coreIntegral.getCriglScore();
			points = Integer.valueOf(criglScore.substring(0, criglScore.length()-1));    //1分
		}
		double fee = 0.0;
		try {
			fee = Double.valueOf((coreRideActive.getCrraeValue()));
		} catch (Exception e) {
		}
		long a = Math.round((points * fee) / 10.0);
		coreRideActiveVO.setKhdScore(Integer.parseInt(a+""));
		//根据凑热闹UUID查询图片附件
		List<CoreAttachment> attachmentslist = coreAttachmentService.findCoreAttachmentByCnd(coreRideActive.getCrraeUuid());
		coreRideActiveVO.setList(attachmentslist);
		
		//获取活动报名状态
		if (!StringUtil.isEmpty(userUuid)) {
			CoreRideRecord coreRideRecord = new CoreRideRecord();
			coreRideRecord.setCrrrdActive(crraeUuid);
			coreRideRecord.setCrrrdUser(userUuid);
			coreRideRecord = coreRideRecordService.getCoreRideRecordByUser(coreRideRecord);
			if (coreRideRecord != null) {
				coreRideActiveVO.setCrraeStatus(coreRideRecord.getCrrrdStatus());
			} else {
				coreRideActiveVO.setCrraeStatus(5);
			}			
		}	
				writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "获取单个信息成功", coreRideActiveVO), response);
		logger.info("[CoreRideActiveController]:end viewsCoreRideActive");
	}

	/**
	* 获取列表<List>	
	* @return
	*/
	@RequestMapping(value="/find/all", method=RequestMethod.POST)
	public void findCoreRideActiveList (HttpServletResponse response) {
		logger.info("[CoreRideActiveController]:begin findCoreRideActiveList");
		List<CoreRideActive> lists = coreRideActiveService.findCoreRideActiveList();
		List<CoreRideActiveVO> vos = new ArrayList<CoreRideActiveVO>();
		CoreRideActiveVO vo;
		//获取相应的积分
		int points = 0;
		CoreIntegral coreIntegral = new CoreIntegral();
		coreIntegral.setCriglUuid("BBBBB");
		coreIntegral = coreIntegralService.getCoreIntegral(coreIntegral);
		if (coreIntegral != null) {
			String criglScore = coreIntegral.getCriglScore();
			points = Integer.valueOf(criglScore.substring(0, criglScore.length()-1));    //1分
		}
		for (CoreRideActive coreRideActive : lists) {
			vo = new CoreRideActiveVO();
			vo.setCrraeUuid(coreRideActive.getCrraeUuid());
			vo.setCrraeTitle(coreRideActive.getCrraeTitle());
			vo.setCrraeContent(coreRideActive.getCrraeContent());
			vo.setCrraeValue(coreRideActive.getCrraeValue());
			vo.setCrraeCount(coreRideActive.getCrraeCount());
			vo.setCrraeCdate(DateUtil.formatTimesTampDate(coreRideActive.getCrraeCdate()));
			vo.setCrraeUdate(DateUtil.formatTimesTampDate(coreRideActive.getCrraeUdate()));
			
			//可获得积分的计算
			double fee = 0.0;
			try {
				fee = Double.valueOf((coreRideActive.getCrraeValue()));
			} catch (Exception e) {
			}
			long a = Math.round((points * fee) / 10.0);
			vo.setKhdScore(Integer.parseInt(a+""));
			//根据凑热闹UUID查询图片附件
			List<CoreAttachment> attachmentslist = coreAttachmentService.findCoreAttachmentByCnd(coreRideActive.getCrraeUuid());
			vo.setList(attachmentslist);
						vos.add(vo);
		}
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "list列表获取成功!", vos),response);
		logger.info("[CoreRideActiveController]:end findCoreRideActiveList");
	}

	/**
	* 获取列表<Page>	
	* @param pageNum 页码
	* @param pageSize 页数
	* 
	* @return
	*/
	@RequestMapping(value="/find/by/cnd", method=RequestMethod.POST)
	public void findCoreRideActivePage (Integer pageNum, Integer pageSize, String crraeTitle, HttpServletResponse response) {
		logger.info("[CoreRideActiveController]:begin findCoreRideActivePage");
		if (pageNum == null || pageNum == 0) {
			pageNum = 1;
		}
		if (pageSize == null || pageSize == 0) {
			pageSize = 10;
		}
		
		int points = 0;
		CoreIntegral coreIntegral = new CoreIntegral();
		coreIntegral.setCriglUuid("BBBBB");
		coreIntegral = coreIntegralService.getCoreIntegral(coreIntegral);
		if (coreIntegral != null) {
			String criglScore = coreIntegral.getCriglScore();
			points = Integer.valueOf(criglScore.substring(0, criglScore.length()-1));    //1分
		}
		
		Page<CoreRideActive> page = coreRideActiveService.findCoreRideActivePage(pageNum, pageSize, crraeTitle);
		Page<CoreRideActiveVO> pageVO = new Page<CoreRideActiveVO>(page.getPageNumber(), page.getPageSize(), page.getTotalCount());
		List<CoreRideActiveVO> vos = new ArrayList<CoreRideActiveVO>();
		CoreRideActiveVO vo;
		for (CoreRideActive coreRideActive : page.getResult()) {
			vo = new CoreRideActiveVO();
			vo.setCrraeUuid(coreRideActive.getCrraeUuid());
			vo.setCrraeTitle(coreRideActive.getCrraeTitle());
			vo.setCrraeContent(coreRideActive.getCrraeContent());
			vo.setCrraeValue(coreRideActive.getCrraeValue());
			vo.setCrraeCount(coreRideActive.getCrraeCount());
			vo.setCrraeCdate(DateUtil.formatTimesTampDate(coreRideActive.getCrraeCdate()));
			vo.setCrraeUdate(DateUtil.formatTimesTampDate(coreRideActive.getCrraeUdate()));
			
			//可获得积分的计算
			double fee = 0.0;
			try {
				fee = Double.valueOf((coreRideActive.getCrraeValue()));
			} catch (Exception e) {
			}
			long a = Math.round((points * fee) / 10.0);
			vo.setKhdScore(Integer.parseInt(a+""));
			//根据凑热闹UUID查询图片附件
			List<CoreAttachment> attachmentslist = coreAttachmentService.findCoreAttachmentByCnd(coreRideActive.getCrraeUuid());
			vo.setList(attachmentslist);
						vos.add(vo);
		}
		pageVO.setResult(vos);
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "page列表获取成功!", pageVO),response);
		logger.info("[CoreRideActiveController]:end findCoreRideActivePage");
	}
//<=================定制内容开始==============
//==================定制内容结束==============>

}